package com.itheima.web.controller.cargo;

import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import com.itheima.common.utils.UtilFuns;
import com.itheima.domain.cargo.ExtCproduct;
import com.itheima.domain.cargo.ExtCproductExample;
import com.itheima.domain.cargo.Factory;
import com.itheima.domain.cargo.FactoryExample;
import com.itheima.service.cargo.ExtCproductService;
import com.itheima.service.cargo.FactoryService;
import com.itheima.web.controller.BaseController;
import com.itheima.web.utils.FileUploadUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Controller
@RequestMapping("/cargo/extCproduct")
public class ExtCproductController extends BaseController {

    @Reference
    ExtCproductService extCproductService;

    @Reference
    FactoryService factoryService;

    @Autowired
    FileUploadUtil fileUploadUtil;

    @RequestMapping(value = "/list",name = "分页查询附件")
    public String list(String contractId,String contractProductId,@RequestParam(defaultValue = "1") int page,@RequestParam(defaultValue = "5") int size){
        //查询
        ExtCproductExample extCproductExample = new ExtCproductExample();
        extCproductExample.createCriteria().andContractProductIdEqualTo(contractProductId);
        PageInfo pageInfo = extCproductService.findAll(extCproductExample, page, size);
        request.setAttribute("page",pageInfo);
        //把contractId和contractProductId存入请求域
        request.setAttribute("contractId",contractId);
        request.setAttribute("contractProductId",contractProductId);
        //设置厂家并保存
        FactoryExample factoryExample = new FactoryExample();
        factoryExample.createCriteria().andCtypeEqualTo("附件");
        List<Factory> factoryList = factoryService.findAll(factoryExample);
        request.setAttribute("factoryList",factoryList);
        return "cargo/extc/extc-list";
    }

    @RequestMapping(value = "/edit",name = "保存或更新附件")
    public String edit(ExtCproduct extCproduct, MultipartFile productPhoto) throws Exception {

        if(UtilFuns.isEmpty(extCproduct.getId())){
            //保存   设置企业id和企业名
            extCproduct.setCompanyId(super.getCurrentUserCompanyId());
            extCproduct.setCompanyName(super.getCurrenUserCompanyName());
            String imgPath = "";
            //判断productPhoto是否有值
            if(productPhoto != null){
                //文件上传
                imgPath = "http://"+fileUploadUtil.upload(productPhoto);
            }
            extCproduct.setProductImage(imgPath);
            extCproductService.save(extCproduct);
        }else {
            //更新
            extCproductService.update(extCproduct);
        }
        return "redirect:/cargo/extCproduct/list.do?contractId="+extCproduct.getContractId()+"&contractProductId="+extCproduct.getContractProductId();

    }

    @RequestMapping(value = "/toUpdate",name = "前往合同附件更新页面")
    public String toUpdate(String id){
        //根据id查询回显
        ExtCproduct extCproduct = extCproductService.findById(id);
        request.setAttribute("extCproduct",extCproduct);
        //查询所有生产厂家
        FactoryExample factoryExample = new FactoryExample();
        factoryExample.createCriteria().andCtypeEqualTo("附件");
        List<Factory> factoryList = factoryService.findAll(factoryExample);
        request.setAttribute("factoryList",factoryList);
        //把contractId和contractProductId存入请求域
        request.setAttribute("contractId",extCproduct.getContractId());
        request.setAttribute("contractProductId",extCproduct.getContractProductId());
        //返回
        return "cargo/extc/extc-update";
    }

    @RequestMapping(value = "/delete",name = "删除附件")
    public String delete(String id,String contractId,String contractProductId){
        extCproductService.delete(id);
        return "redirect:/cargo/extCproduct/list.do?contractId="+contractId+"&contractProductId="+contractProductId;
    }
}
